package Euler32;

import java.util.*;

import ReusableCode.MathFunctions;

import com.google.common.collect.Collections2;

public class PandigitalProducts {
	public static void main(String[] args) {
		Iterator<List<String>> iter = Collections2.permutations( Arrays.asList("1 2 3 4 5 6 7 8 9".split(" ")) ).iterator();
		List<Long> perms = new ArrayList<Long>();
		
		System.out.println("Calculating...");
		
		while(iter.hasNext())
		{
			String numStr = "";
			for(Iterator<String> permIter = iter.next().iterator(); permIter.hasNext();)
			{
				numStr += permIter.next();
			}
			
			long num = Long.parseLong(numStr);
			
			for(int i = 1; i < numStr.length()/2+1; i++)
			{
				long multiplicand = (long)(num / Math.pow(10, (numStr.length()-i)));
				boolean shouldBreak = false;
				
				for(int j = i+1; j < numStr.length()-1; j++)
				{
					long product = (long)(num % Math.pow(10, (numStr.length()-j)));
					long multiplier = (long)((num % Math.pow(10, (numStr.length()-i))) / (Math.pow(10, (numStr.length()-j))));
					
					if((multiplicand * multiplier) == product)
					{
						if(!perms.contains(product)){
							perms.add(product);
						}
						shouldBreak=true;
						break;
					}
				}
				
				if(shouldBreak)
				{
					break;
				}
			}
		}
		System.out.println(perms);
		System.out.println("Size is: " + perms.size());
		System.out.println("Sum is: " + MathFunctions.getSetSumLong(perms));
	}
}
